package hydrap2p.gui.commands;

import hydrap2p.download.DownloadState;
import hydrap2p.gui.FileTransferPanel;
import hydrap2p.gui.GUIDataHandler;
import hydrap2p.gui.GUIDownloadListing;

import java.util.ArrayList;
import java.util.List;

import org.apache.thrift.TException;

public class Cmd_FileTransferPanel_PauseTransfer implements Command {

	@Override
	public void execute() {
		List<Integer> indexList = new ArrayList<Integer>();
		
		// Put selected indices in a sortable list
		int[] indices = FileTransferPanel.getInstance().getTable().getSelectedRows();
		
		// For each row, change the status 
		GUIDownloadListing gdl;
		for(int i : indices){
			// TODO Change how the GDLs are accessed in the GUIDataHandler
			gdl = GUIDataHandler.getInstance().getDownloadListing().get(i);
			
			try {
				GUIDataHandler.getInstance().setDownloadState(gdl, DownloadState.PAUSED);
				gdl.setStatus(DownloadState.PAUSED.toString());
			} catch (TException te) {
				System.out.println("Failed to pause download: " + gdl.getName());
				te.printStackTrace();
			}
			
		}
		
		// Refresh data transfer table
		FileTransferPanel.getInstance().refreshDownloadList();
		
		System.out.println(getCommand());
		
	}

	@Override
	public String getCommand() {
		return this.getClass().getSimpleName();
	}

	@Override
	public void unexecute() {
		// TODO Auto-generated method stub

	}

}
